/**
 * Created by GCX on 2018/5/30.
 */
$(function(){

    layui.use(['layer','form'],function(){

        var layer = layui.layer;
        var form = layui.form();


        var SUB_NAME,
            SUB_TYPE;

        var attachmentData = new Array();	//定义存放附件的对象

        // 定义webuploader配置
        var webuploader_option = {
            auto: false,
            server: '/BBS/upload',
            pick: '#filePicker',
            fileVal: 'Filedata',
            accept: {
                title: 'file',
                extensions: 'zip,rar,jpg,png,jpeg,doc,xls,gif,xlsx,docx,pdf'
            },
            method: 'POST',
        };

        // 实例化webuploader
        var uploader = WebUploader.create(webuploader_option);

        // 添加到队列时
        uploader.on('fileQueued', function (file) {
            var fileItem = '<div class="file-item" id="' + file.id + '">' +
                '<div class="pull-left"><i class="file-icon file-icon-ext-' + file.ext + '"></i> ' + file.name + '</div>' +
                '<div class="pull-right"><i class="icon-remove" title="移除该文件"></i></div>' +
                '<div class="clearfix"></div>' +
                '</div>';
            $(webuploader_option.pick).next().append(fileItem);
        });

        // 上传之前
        uploader.on('uploadBeforeSend', function (block, data, headers) {

            data.SUB_NAME = SUB_NAME;
            data.DIR = '/BBS/';

        });

        //上传成功时
        uploader.on('uploadSuccess', function (file, response) {	//文件了上传成功后触发

            if (response.flag > 0) {
                //当文件上传成功后添加到attachmentData
                var attachmentObject = new Object();
                attachmentObject.savename = response.savename;
                attachmentObject.ext = response.ext;
                attachmentObject.path = response.path;
                attachmentData.push(attachmentObject);
            } else {
                layer.closeAll();
                layer.msg(response.msg, {icon: 2, time: 2000});
            }
        });

        // 所有文件上传结束时
        uploader.on('uploadFinished', function () {
            $.ajax({
                url: '/BBS/insertAttachment',
                type: 'POST',
                data: {
                    SUB_NAME: SUB_NAME,
                    SUB_TYPE: SUB_TYPE,
                    attachments: JSON.stringify(attachmentData)
                },
                dataType: 'json',
                success: function (response) {
                    if (response.flag > 0) {
                        layer.closeAll();
                        layer.msg(response.msg, {icon: 1, time: 2000});
                        setTimeout(function () {
                            location.href = '/BBS/detail/id/' + SUB_NAME;
                        }, 2000);
                    } else {
                        layer.closeAll();
                        layer.msg(response.msg, {icon: 2, time: 2000});
                    }
                }
            });
        });

        // 上传错误时
        uploader.on('error', function (code) {
            var msg = '';
            switch (code) {
                case 'Q_EXCEED_NUM_LIMIT':
                    msg = '只能上传一个文件';
                    break;
                case 'Q_EXCEED_SIZE_LIMIT':
                    msg = '文件大小超出限制';
                    break;
                case 'Q_TYPE_DENIED':
                    msg = '文件格式不允许';
                    break;
                case 'F_DUPLICATE':
                    msg = '文件已存在';
                    break;
                default:
                    msg = code;
            }
            layer.msg(msg, {icon: 2, time: 2000});
        });

        $('.uploader-file-queue').on('click', '.icon-remove', function () {

            var id = $(this).parent().parent().attr('id');
            // 删除队列中的文件
            uploader.removeFile( id, true );
            // 删除dom节点
            $(this).parent().parent().remove();

        });


        /**
         * 提交数据
         */
        form.on('submit(publish)', function(data) {
            var loading = layer.load(2, {
                shade: [0.5, '#fff'] //0.1透明度的白色背景
            });

            $.post('/BBS/store', data.field, function(response) {
                if (response.flag > 0) {
                    SUB_NAME = response.id;
                    SUB_TYPE = response.type;
                    if (uploader.getFiles().length > 0) {
                        uploader.upload();
                    } else {
                        location.href = '/BBS/detail/id/' + response.id;
                    }
                } else {
                    layer.close(loading);
                    layer.alert(response.msg);
                }
            }, 'json');
        })

    })

})